package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* OneLineTwoCapView
* */
class OneLineTwoCapView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.STROKE
            strokeCap = Paint.Cap.SQUARE
            color = context.resources.getColor(R.color.color_00BFA5)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.STROKE
            strokeCap = Paint.Cap.ROUND
            color = context.resources.getColor(R.color.color_00BFA5)
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2

            save()
            translate(cx, cy)

            val mStrokeWidth = (cx * 1 / 6).coerceAtLeast(minStrokeWidth)
            mPaint0.strokeWidth = mStrokeWidth
            mPaint1.strokeWidth = mStrokeWidth


            drawLine(
                0F,
                -cy + mStrokeWidth / 2,
                0F,
                cy - mStrokeWidth / 2,
                mPaint1
            )

            drawLine(
                -cx + mStrokeWidth / 2,
                0F,
                0F,
                0F,
                mPaint1
            )


            drawLine(
                cx * 5 / 14,
                0F,
                cx * 5 / 7,
                0F,
                mPaint0
            )

            drawLine(
                cx * 5 / 7,
                0F,
                cx - mStrokeWidth / 2,
                0F,
                mPaint1
            )

            restore()
        }
    }
}